# LightingSystem.gd - 全局光照系统管理器
# 《光影之间》- 光影效果和影视能力管理

extends Node

# 光影状态枚举
enum LightState {
	NORMAL,
	SHADOW_VISION,
	LIGHT_ENHANCED,
	SHADOW_ENHANCED
}

# 当前光影状态
var current_state := LightState.NORMAL
var shadow_vision_active := false

# 光影对象组
var light_sources := []
var shadow_zones := []
var light_platforms := []

# 影视效果参数
var shadow_vision_intensity := 0.8
var shadow_vision_contrast := 2.0
var shadow_vision_color := Color(0.2, 0.2, 0.8, 0.3)

# 信号
signal lighting_state_changed(new_state: LightState)
signal shadow_vision_toggled(is_active: bool)
signal light_source_added(light_source: Node)
signal shadow_zone_added(shadow_zone: Node)

func _ready():
	print("光照系统已初始化")

# 设置光影状态
func set_lighting_state(new_state: LightState):
	if current_state != new_state:
		current_state = new_state
		lighting_state_changed.emit(new_state)
		update_all_lighting_objects()
		print("光影状态变更为: ", LightState.keys()[new_state])

# 切换影视模式
func toggle_shadow_vision(is_active: bool):
	shadow_vision_active = is_active
	shadow_vision_toggled.emit(is_active)
	
	if is_active:
		set_lighting_state(LightState.SHADOW_VISION)
	else:
		set_lighting_state(LightState.NORMAL)

# 注册光源
func register_light_source(light_source: Node):
	if light_source not in light_sources:
		light_sources.append(light_source)
		light_source_added.emit(light_source)
		print("注册光源: ", light_source.name)

# 注册阴影区域
func register_shadow_zone(shadow_zone: Node):
	if shadow_zone not in shadow_zones:
		shadow_zones.append(shadow_zone)
		shadow_zone_added.emit(shadow_zone)
		print("注册阴影区域: ", shadow_zone.name)

# 注册光平台
func register_light_platform(platform: Node):
	if platform not in light_platforms:
		light_platforms.append(platform)
		print("注册光平台: ", platform.name)

# 移除光源
func unregister_light_source(light_source: Node):
	if light_source in light_sources:
		light_sources.erase(light_source)
		print("移除光源: ", light_source.name)

# 移除阴影区域
func unregister_shadow_zone(shadow_zone: Node):
	if shadow_zone in shadow_zones:
		shadow_zones.erase(shadow_zone)
		print("移除阴影区域: ", shadow_zone.name)

# 移除光平台
func unregister_light_platform(platform: Node):
	if platform in light_platforms:
		light_platforms.erase(platform)
		print("移除光平台: ", platform.name)

# 更新所有光照对象
func update_all_lighting_objects():
	# 更新光源
	for light_source in light_sources:
		if light_source.has_method("update_lighting_state"):
			light_source.update_lighting_state(current_state)
	
	# 更新阴影区域
	for shadow_zone in shadow_zones:
		if shadow_zone.has_method("update_lighting_state"):
			shadow_zone.update_lighting_state(current_state)
	
	# 更新光平台
	for platform in light_platforms:
		if platform.has_method("update_lighting_state"):
			platform.update_lighting_state(current_state)

# 检查位置是否在光照区域
func is_position_in_light(position: Vector2) -> bool:
	for light_source in light_sources:
		if light_source.has_method("is_position_illuminated"):
			if light_source.is_position_illuminated(position):
				return true
	return false

# 检查位置是否在阴影区域
func is_position_in_shadow(position: Vector2) -> bool:
	for shadow_zone in shadow_zones:
		if shadow_zone.has_method("contains_position"):
			if shadow_zone.contains_position(position):
				return true
	return false

# 获取位置的光照强度
func get_light_intensity_at_position(position: Vector2) -> float:
	var total_intensity := 0.0
	
	for light_source in light_sources:
		if light_source.has_method("get_intensity_at_position"):
			total_intensity += light_source.get_intensity_at_position(position)
	
	return min(total_intensity, 1.0)

# 创建临时光源
func create_temporary_light(position: Vector2, intensity: float, duration: float):
	# TODO: 创建TemporaryLight场景后启用此功能
	# var temp_light = preload("res://scenes/effects/TemporaryLight.tscn").instantiate()
	
	# 临时使用PointLight2D节点
	var temp_light = PointLight2D.new()
	get_tree().current_scene.add_child(temp_light)
	temp_light.global_position = position
	temp_light.energy = intensity
	temp_light.color = Color.WHITE
	
	register_light_source(temp_light)
	
	# 设置定时器移除临时光源
	await get_tree().create_timer(duration).timeout
	unregister_light_source(temp_light)
	temp_light.queue_free()

# 创建光桥
func create_light_bridge(start_pos: Vector2, end_pos: Vector2, width: float = 64.0):
	# TODO: 创建LightBridge场景后启用此功能
	# var light_bridge = preload("res://scenes/objects/LightBridge.tscn").instantiate()
	
	# 临时使用简单的StaticBody2D节点
	var light_bridge = StaticBody2D.new()
	var collision_shape = CollisionShape2D.new()
	var rect_shape = RectangleShape2D.new()
	
	get_tree().current_scene.add_child(light_bridge)
	light_bridge.add_child(collision_shape)
	collision_shape.shape = rect_shape
	
	# 设置桥的位置和大小
	var bridge_length = start_pos.distance_to(end_pos)
	rect_shape.size = Vector2(bridge_length, width)
	light_bridge.global_position = (start_pos + end_pos) / 2
	light_bridge.rotation = start_pos.angle_to_point(end_pos)
	
	register_light_platform(light_bridge)
	return light_bridge

# 获取影视效果参数
func get_shadow_vision_shader_params() -> Dictionary:
	return {
		"intensity": shadow_vision_intensity,
		"contrast": shadow_vision_contrast,
		"color": shadow_vision_color,
		"is_active": shadow_vision_active
	}

# 设置影视效果强度
func set_shadow_vision_intensity(intensity: float):
	shadow_vision_intensity = clamp(intensity, 0.0, 1.0)

# 设置影视效果对比度
func set_shadow_vision_contrast(contrast: float):
	shadow_vision_contrast = clamp(contrast, 1.0, 5.0)

# 设置影视效果颜色
func set_shadow_vision_color(color: Color):
	shadow_vision_color = color

# 清理所有光照对象
func clear_all_lighting_objects():
	light_sources.clear()
	shadow_zones.clear()
	light_platforms.clear()
	print("已清理所有光照对象")

# 获取光照系统状态
func get_lighting_system_state() -> Dictionary:
	return {
		"current_state": current_state,
		"shadow_vision_active": shadow_vision_active,
		"light_sources_count": light_sources.size(),
		"shadow_zones_count": shadow_zones.size(),
		"light_platforms_count": light_platforms.size()
	}